﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Skin2D.Bone;

namespace Skin2D.Control
{
    class CtrBoneListTreeView
    {

        public CtrBoneListTreeView(Panel container,Bone2D data)
        {
            m_container = container;
            m_data = data;
            m_TreeNodeMap = new Dictionary<Bone2D, TreeNode>();
            Init();
        }

        public void InitTreeNode(Bone2D data,TreeNode root)
        {
            TreeNode node = new TreeNode("Bone " + data.ID.ToString());
            node.Tag = data;
            if (root != null)
                root.Nodes.Add(node);
            else
                m_treeView.Nodes.Add(node);

            m_TreeNodeMap[data] = node;

            foreach (Bone2D item in data.Childs())
            {
                InitTreeNode(item, node);
            }

        }

        private void Init()
        {
            m_treeView = new TreeView();
            m_treeView.Dock = DockStyle.Fill;
            m_treeView.BorderStyle = BorderStyle.Fixed3D;
            m_treeView.Location = new System.Drawing.Point(0, 0);
            m_treeView.Name = "CtrBoneTreeView";
            
            m_container.Controls.Clear();
            m_container.Controls.Add(m_treeView);

            InitTreeNode(m_data, null);

            m_treeView.AfterSelect += new TreeViewEventHandler(m_treeView_AfterSelect);

        }

        void m_treeView_AfterSelect(object sender, TreeViewEventArgs e)
        {
            Bone2D bone = e.Node.Tag as Bone2D;
            if (OnChangeSelected != null && bone != null)
            {
                OnChangeSelected(bone);

                if(m_selected!=null)
                    m_selected.Selected = false;
                bone.Selected = true;

                m_selected = bone;
            }
        }

        public void Select(Bone2D bone)
        {
            if (bone == null)
            {
                if (m_selected != null)
                    m_selected.Selected = false;
            }
            else
            {
                m_treeView.SelectedNode = m_TreeNodeMap[bone];
                if (OnChangeSelected != null && bone != null)
                {
                    OnChangeSelected(bone);

                    if (m_selected != null)
                        m_selected.Selected = false;
                    bone.Selected = true;

                    m_selected = bone;
                }
            }
        }

        public Bone2D getSelected()
        {
            return m_selected;
        }

        public TreeNode getSelectedNode()
        {
            return m_TreeNodeMap[m_selected];
        }

        public TreeNode getTreeNode(Bone2D bone)
        {
            return m_TreeNodeMap[bone];
        }

        public void RemoveBone(Bone2D bone, Bone2D child)
        {
            TreeNode root = m_TreeNodeMap[bone];
            TreeNode node = m_TreeNodeMap[child];

            root.Nodes.Remove(node);
            m_TreeNodeMap.Remove(child);
        }

        public void ReAddTreeNode(Bone2D bone, TreeNode node,Bone2D readd_bone,int child_index)
        {
            TreeNode root = m_TreeNodeMap[bone];
            if (root != null)
            {
                root.Nodes.Insert(child_index, node);
                m_TreeNodeMap[readd_bone] = node;
            }
        }

        public event Action<Bone2D> OnChangeSelected;

        private TreeView m_treeView;
        private Panel m_container;
        private Bone2D m_data;
        private Bone2D m_selected;
        public Dictionary<Bone2D, TreeNode> m_TreeNodeMap;
    }
}
